/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "Application.hpp"
#include "Mainframe.hpp"

// This macro ensures a main function is present.
IMPLEMENT_APP(Application)

// Constructor is used for initialization only
Application::Application()
: m_lang(wxLANGUAGE_UNKNOWN)
{
}

void  Application::ChangeLanguage(bool force)
{
	// Get Config
	wxConfig * config = new wxConfig();
	m_lang = static_cast<wxLanguage>(config->ReadLong("Language", wxLANGUAGE_UNKNOWN));

	// This is for calling it from inside the program. So we can get rid of the "wrong" language.
	if (force){
		m_lang = wxLANGUAGE_UNKNOWN;
	}

	// Get desired Language from user based on available Languages
	if ( m_lang == wxLANGUAGE_UNKNOWN )
	{
		int choice = wxGetSingleChoiceIndex(wxT("Please select Language"), 
					 wxT("Choose Translation"), 
					 WXSIZEOF(LangNames),
					 LangNames);
		m_lang = (choice == -1 ? wxLANGUAGE_DEFAULT : LangID[choice]);
	}
	// Init
	if ( !m_loc.Init(m_lang, wxLOCALE_CONV_ENCODING) )
	{
		wxLogWarning(_("This language is not supported by the system."));
	}

	// Install Catalogue
	const wxLanguageInfo* pInfo = wxLocale::GetLanguageInfo(m_lang);
	if (!m_loc.AddCatalog("internat"))
	{
		wxLogError(wxT("Couldn't find/load the 'internat' catalog for locale '%s'."),
			pInfo ? pInfo->GetLocaleName() : _("unknown"));
	}
	m_loc.AddCatalog("wxstd.mo");

	// Modify Config
	config->Write("Language", static_cast<long>(m_lang));
	delete config;
}

// Creates the frame
bool Application::OnInit()
{
	// Setting Names for Config and stuff
	this->SetVendorName(wxT("Scorched Productions"));
	this->SetAppName(wxT("AwesOme Script"));
	this->SetAppDisplayName(wxT("AwesOme Script"));
	this->SetVendorDisplayName(wxT("Scorched Productions"));

	wxCmdLineParser parser(argc, argv);
	parser.AddSwitch("uninstall");
	parser.Parse();
	if ( parser.Found("uninstall") )
	{
		if ( wxMessageBox("Are you sure you want to delete all keys from the registry?", wxMessageBoxCaptionStr, wxYES_NO) == wxYES )
		{
			wxConfig* cfg = new wxConfig();
			cfg->DeleteAll();
			delete cfg;
			wxMessageBox("All keys have been deleted. Please restart the program now without command line parameter.");
			return false;
		}
		else
		{
			wxMessageBox("No keys have been removed. Program starting normally.");
		}		
	}

	// Change Language if necessary and install files
	ChangeLanguage(false);

	// Install Image Handler for PNG
	wxImage::AddHandler(new wxPNGHandler);

	// Init Curl
	curl_global_init(CURL_GLOBAL_WIN32);

	// Creating Mainframe
	Mainframe* f = new Mainframe(wxDEFAULT_FRAME_STYLE);
	f->Show(true);
	SetTopWindow(f);
	return true;
}

const wxString Application::GetLanguage() const
{
	return wxLocale::GetLanguageName(m_lang);
}

int Application::OnExit()
{
	curl_global_cleanup();
	return 0;
}